package com.shenhangye.pushlive

import android.graphics.Point
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.shenhangye.pushlive.camera.view.CameraGlSurfaceview
import com.shenhangye.pushlive.mp4.Mp4Decoder


class MainActivity : AppCompatActivity() {
    private  val TAG = "MainActivity"

    var mMp4Decoder: Mp4Decoder?=null
    var mCameraGlSurfaceview: CameraGlSurfaceview?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        mCameraGlSurfaceview=findViewById(R.id.camera_recorde)
        findViewById<Button>(R.id.start_recorder).setOnClickListener {
            mCameraGlSurfaceview?.startRecorde()

        }

        findViewById<Button>(R.id.stop_recorder).setOnClickListener {
            mCameraGlSurfaceview?.onStop()
        }

        mMp4Decoder=Mp4Decoder("/storage/sdcard0/avier1.mp4",lifecycle)
        val display = windowManager.defaultDisplay
        val point = Point()
        display.getSize(point)
        val width = display.width
        val height = display.height
        Log.e(TAG, "width222: $width,height:$height")
        // 检查相机权限
        XXPermissions.with(this)
            // 申请单个权限
            .permission(Permission.CAMERA)
            .permission(Permission.MANAGE_EXTERNAL_STORAGE)

            .permission(Permission.RECORD_AUDIO)
//            .permission(Permission.READ_MEDIA_IMAGES)
//            .permission(Permission.READ_MEDIA_VIDEO)
//            .permission(Permission.READ_MEDIA_AUDIO)
            // 申请多个权限

            // 设置权限请求拦截器（局部设置）
            //.interceptor(new PermissionInterceptor())
            // 设置不触发错误检测机制（局部设置）
            //.unchecked()
            .request(object : OnPermissionCallback {

                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (!allGranted) {
//                        toast("获取部分权限成功，但部分权限未正常授予")
                        return
                    }
//                    toast("获取录音和日历权限成功")
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    if (doNotAskAgain) {
//                        toast("被永久拒绝授权，请手动授予录音和日历权限")
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(applicationContext, permissions)
                    } else {
//                        toast("获取录音和日历权限失败")
                    }
                }
            })
    }
}